package edu.mtu.citizenscience.mushroommapper.model;

import com.openswimsoftware.orm.model.Model;
import com.openswimsoftware.orm.model.annotation.Children;
import com.openswimsoftware.orm.model.exception.InvalidModelField;
import com.openswimsoftware.orm.model.list.ModelList;
import com.openswimsoftware.orm.model.query.where.WhereOperator;

import android.content.Context;

import edu.mtu.citizenscience.mushroommapper.model.family.ModelGenus;

/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Model of a mushroom family in the app DB has a name and genera
 */
public class ModelFamily extends Model<ModelFamily> {
	// reference var for passing family model id between activities
	public static final String FAMILY_ID = "FAMILY_ID";
	
	// the family model name
	private String name;
	
	// the family has many child genera models
	@Children(modelType = ModelGenus.class)
	private ModelList<ModelGenus> genera;
	
	/**
	 * Create the family model
	 * 
	 * @param   context - Context - context for model operations
	 * @return	void
	 */
	public ModelFamily(Context context) 
	{
		super(context, ModelFamily.class);
	}

	/**
	 * Create the family model
	 * 
	 * @param   context - Context - context for model operations
	 * @param	id - long - the id of the model to load
	 * @return	void
	 */
	public ModelFamily(Context context, long id) 
	{
		super(context, ModelFamily.class, id);
	}

	/**
	 * Get the family name
	 * 
	 * @param   void
	 * @return	String - the family name
	 */
	public String getName() 
	{
		// make sure the model is loaded
		this.load();
		
		return this.name;
	}
	
	/**
	 * Set the family name
	 * 
	 * @param   String - the family name
	 * @return	void
	 */
	public void setName(String name) 
	{
		// mark the model as updated
		this.updated = true;
		
		this.name = name;
	}
	
	/**
	 * Get the family genus models
	 * 
	 * @param   void
	 * @return	ModelList<ModelGenus> - List of genus models
	 */
	public ModelList<ModelGenus> getGenera() 
	{
		// make sure the model is loaded
		this.load();
		
		return this.genera;
	}
	
	/**
	 * Set the family genus models
	 * 
	 * @param   ModelList<ModelGenus> - List of genus models
	 * @return	void
	 */
	public void setGenera(ModelList<ModelGenus> genera) 
	{
		// mark the model as updated
		this.updated = true;
		
		this.genera = genera;
	}
	
	/**
	 * Search for the name in family models
	 * 
	 * @param   name - String - the family name to find
	 * @return	ModelFamily - model family found for the name
	 */
	public ModelFamily find(String name)
	{
		try 
		{
			// return family model where name equals the passed name
			return this.findQuery().where("name", WhereOperator.EQUAL, name).execute();
		} 
		catch (InvalidModelField e) 
		{
			// happens if model is not valid
			e.printStackTrace();
		}
		
		// if a problem return nothing
		return null;
	}

}
